package com.example.demo;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import com.example.demo.adapter.DealsAdapter;
import com.example.demo.application.DealDroidApp;
import com.example.demo.model.Item;
import com.example.demo.model.Section;

import java.util.*;

public class DealList extends ListActivity {
    private static final String TAG = "DAILY_DEALS_APP";
    private static final int MENU_REPARSE = 0;

    private DealDroidApp app;
    private List<Item> items;
    private DealsAdapter dealsAdapter;
    private Spinner sectionSpinner;
    private ArrayAdapter<Section> spinnerAdapter;
    private int currentSelectedSection;
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deal_list);

        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage(getString(R.string.deal_list_retrieving_data));

        app = (DealDroidApp) getApplication();

        items = new ArrayList<Item>();
        dealsAdapter = new DealsAdapter(items);
        setListAdapter(dealsAdapter);
        if (app.getSectionList().isEmpty()) {
            if (app.connectionPresent()) {
                new ParseFeedTask().execute();
            } else {
                Toast.makeText(this, getString(R.string.deal_list_network_unavailable), Toast.LENGTH_LONG).show();
            }
        } else {
            resetListItems(app.getSectionList().get(0).getItems());
        }

        spinnerAdapter = new ArrayAdapter<Section>(DealList.this, android.R.layout.simple_spinner_item, app.getSectionList());
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sectionSpinner = (Spinner) findViewById(R.id.section_spinner);
        sectionSpinner.setAdapter(spinnerAdapter);

        sectionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (currentSelectedSection != position) {
                    currentSelectedSection = position;
                    resetListItems(app.getSectionList().get(position).getItems());
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) { }
        });

    }


    private void resetListItems(List<Item> newItems) {
        items.clear();
        items.addAll(newItems);
        dealsAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        view.setBackgroundColor(R.color.background_light);
        app.setCurrentItem(app.getSectionList().get(currentSelectedSection).getItems().get(position));
        Intent dealDetails = new Intent(DealList.this, DealDetails.class);
        startActivity(dealDetails);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, DealList.MENU_REPARSE, 0, R.string.deal_list_reparse_menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_REPARSE:
                if (app.connectionPresent()) {
                    new ParseFeedTask().execute();
                } else {
                    Toast.makeText(this, getString(R.string.deal_list_network_unavailable), Toast.LENGTH_LONG).show();
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPause() {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        super.onPause();
    }


    private class DealsAdapter extends ArrayAdapter<Item> {

        public DealsAdapter() {
            super(DealList.this, R.layout.list_item, new ArrayList<Item>());
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_item, parent, false);
            }
        }
    }
}
